// JavaScript Document
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=2007;
var maxYear=2010;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

// Kiem tra ngay theo chuan (dd/mm/yyyy)
function checkExistVal(str, mark)
{
	for(i = 0; i < str.length; i++)
		if (mark.indexOf(str.charAt(i)) < 0) return false;
	return 	true;
}
function countChar(str, mark)
{
	var Count = 0;
	for(i = 0; i < str.length; i++)
		if (str.charAt(i) == mark) Count++;
	return 	Count;
}
function getYearToInt(strDate)
{
	i = strDate.lastIndexOf("/");
	strYear = strDate.substring(i + 1, strDate.length);
	return stringToInt(strYear);
}
function getMonthToInt(strDate)
{
	i = strDate.lastIndexOf("/");
	j = strDate.lastIndexOf("/", i - 1) ;	
	strMonth = strDate.substring(j + 1, i);
	return stringToInt(strMonth);
}
function getDateToInt(strDate)
{
	k = strDate.indexOf("/");			
	strDay = strDate.substring(0, k);
	return stringToInt(strDay);
}
function checkIntVal(val, low, high)
{
	if (val <= high && val >= low) return true;
	return false;
}
function stringToInt(str)
{
	if(!stringIsNum(str))	return 0 ;
	if(str.charAt(0) == '0') return stringToInt(str.substring(1, str.length));
	else if(str.length > 0) return parseInt(str);
 else return (0);
}
function stringIsNum(val)
{
	var str = new String(val);
	for (i=0;i<str.length;i++) 
		if (isNaN(parseInt(str.charAt(i))))
			return false;
	return true;
}
function checkFormatDate(strDate)
{
	if(!checkExistVal(strDate, '1234567890/') || countChar(strDate, '/') != 2) return false;
	Year = getYearToInt(strDate);
	Month = getMonthToInt(strDate);
	Day = getDateToInt(strDate);
	if(!checkIntVal(Month, 1, 12))	return false;	
	if(!checkIntVal(Day, 1, 31)) return false;	
	
	if (Month == 4 || Month == 6 || Month == 9 || Month == 11 )
		if(Day > 30)	return false;
		
	if(((Year % 4 == 0) && (Year %100 != 0)) || Year % 400 == 0)
	{
		if((Month == 2 ) && ( Day > 29))	return false;
	} 
	else	if((Month == 2 ) && ( Day > 28))	return false;
	return 	true;
}